#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年9月8日

@author: chenyongbing
'''
import sys,os
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

'''
    获取访问 门店 A 的顾客 同时也访问 其他门店 的 人数、停留时长

'''
import logging
sys.path.append(os.path.join(current_dir,'../'))
from base.retail_datas import myRetailDatas
from base.time_method import myTimeMethod

def get_sum_dur_from_roledata_by_macs(roledata,macs=[]):
    sum_dur = 0
    for mac,macdatas in roledata.items():
        if mac not in macs:continue
        for macdata in macdatas:
            dur = macdata['dur']
            sum_dur+=int(dur)
    return sum_dur
        

def getRoleFromHiveFile(day=None,groups = [],rawpath='/Udisk/new_role_rerun/',timeRange=None,roles=[1,-1,3,10],EMPLOYEES=[],minDur=60,maxDur=24*60*60):
    rfile = '%s/%s/part-00000'%(rawpath,day)
    #2015-08-01,32013138,d8:bb:2c:f0:60:73,2015-08-01 18:10:48,2015-08-01 18:10:48,0,1,Apple
    datas = {}
    if timeRange!=None and re.search('[\d]{2}:[\d]{2}:[\d]{2},[\d]{2}:[\d]{2}:[\d]{2}',timeRange):
        min_time,max_time = timeRange.split(',')
    elif timeRange == None:
        min_time = '00:00:00'
        max_time = '23:59:59'
    else:
        logging.error('timeRange %s fromat Error,use default min and max time.'%timeRange)
        min_time = '00:00:00'
        max_time = '23:59:59'
    
    if not os.path.exists(rfile):
        logging.error('role file %s not exists'%rfile)
        return datas
    for line in open(rfile).read().split('\n'):
        pass
        lineList = line.split(',')
        if len(lineList) < 8:continue
        group = lineList[1]
        if int(group) not in groups and str(group) not in groups:continue
        mac = lineList[2]
        st = lineList[3][11:]
        et = lineList[4][11:]
        
        if st < min_time or st > max_time:
            logging.warn('st %s not in (%s,%s)'%(st,min_time,max_time))
            continue 
        if  mac in EMPLOYEES:
            logging.info('mac %s is employee.'%mac)
            continue
        dur = int(lineList[5])
        if dur<minDur*1000 or dur>maxDur*1000:continue
        role = lineList[6]
        if int(role) not in roles and role not in roles:continue
        fac = lineList[7]
        if group not in datas.keys():
            datas[group] = {}
        if mac not in datas[group].keys():
            datas[group][mac] = []
            
        datas[group][mac].append({'st':st,'et':et,'dur':dur,'role':role,'fac':fac})
    return datas




def get_sum_dur(macdatas):
    sum_dur = 0
    for data in macdatas:
        sum_dur+=data['dur']
    return sum_dur

def getEmployee(st=None,et=None,comp_id=None,timeRange='09:30:00,16:30:00',roles=[1,-1,3,10]):
    EMPLOYEES= []
    groups = myRetailDatas.get_groups_by_comp(comp_id=comp_id)
    logging.info('groups %s'%len(groups))
    days  = myTimeMethod.get_slist_between_st_et(st, et)
    datas = {}
    for day in days:
        roledatas = getRoleFromHiveFile(day=day, groups=groups,timeRange = timeRange,roles=roles)
        logging.info('roledatas length %s'%len(roledatas))
        
        for group , macsdatas in roledatas.items():
            if group not in datas.keys():
                datas[group] = {}
            groupdatas = datas[group]
                
            for mac , macdetails in macsdatas.items():
                if mac not in groupdatas.keys():
                    groupdatas[mac] = []
                groupdatas[mac]+=macdetails
    for group , groupdatas in datas.items():
        for mac , macdatas in groupdatas.items():
            if len(macdatas)>=8 or get_sum_dur(macdatas)>=4*60*60*1000:
                EMPLOYEES.append(mac)
    logging.info('Found Employees %s'%len(EMPLOYEES))
    return EMPLOYEES
        
        
def merger_group_datas(alldatas={},groups=[]):
    datas = {}
    for group , groupdatas in alldatas.items():
        if group not in groups:continue
        for mac , macdatas in groupdatas.items():
            if mac not in datas.keys():
                datas[mac] = []
            datas[mac]+=macdatas
    return datas


def stat_cross_shops_flow(st=None,et=None,first_groups=[],cross_groups=[],comp_id=None,timeRange='09:30:00,16:30:00',EMPLOYEES=[],roles=[1,-1,3,10],minDur = 60,maxDur=24*60*60):
    if len(first_groups) == 0 or len(cross_groups)==0 or comp_id==None:
        logging.error('You Must Enter A Exists Shop Group.')
    group_name = myRetailDatas.get_group_name(group=first_groups[0])
    print ','.join([u'日期',u'%s 到访人次'%group_name,u'%s 到访人数'%group_name,u'交叉门店',u'交叉访问人数',u'交叉门店到访人数',u'交叉访问占比',u'停留总时长(秒)',u'停留平均时长(秒)'])
    groups = myRetailDatas.get_groups_by_comp(comp_id=comp_id)
    groups = list(set(groups+cross_groups))
    days  = myTimeMethod.get_slist_between_st_et(st, et)
    for day in days:
        roledatas = getRoleFromHiveFile(day=day, groups=groups,timeRange = timeRange,roles=roles,EMPLOYEES=EMPLOYEES,minDur=minDur,maxDur=maxDur)
        logging.info('roledatas length %s'%len(roledatas))

        
        groupdatas = merger_group_datas(roledatas,first_groups)
        cross_groupdatas = merger_group_datas(roledatas,cross_groups)
        groupmacs = groupdatas.keys()
        cross_groupmacs = cross_groupdatas.keys()
        samemacs = list(set(cross_groupmacs).intersection(set(groupmacs)))
        flow_count = 0
        for groupdata in groupdatas.values():
            flow_count+=len(groupdata)
        
        flow_num = len(groupmacs)
        same_flow_num = len(samemacs)
        if same_flow_num == 0:
            same_flow_num = 1
        flow_sum_dur = get_sum_dur_from_roledata_by_macs(groupdatas,samemacs)/1000
        cross_flow_num = len(cross_groupmacs)
        #print day,shop_group,len(cross_groupmacs),flow_num,flow_sum_dur
        cross_group_name = myRetailDatas.get_group_name(group=cross_groups[0])
        print ','.join([day,str(flow_count),str(flow_num),cross_group_name,str(same_flow_num),str(cross_flow_num),str(round(float(same_flow_num)/flow_num,2)),str(flow_sum_dur),str(flow_sum_dur/same_flow_num)])
    
            

        
def export_mac(st=None,et=None,comp_id=None,timeRange='09:30:00,16:30:00',EMPLOYEES=[],roles=[1,-1,3,10],minDur=0,maxDur=24*60*60,exportPath='log/macs/'):
    if comp_id==None:
        logging.error('You Must Enter A Exists Shop Group.')
    
    groups = myRetailDatas.get_groups_by_comp(comp_id=comp_id)
    days  = myTimeMethod.get_slist_between_st_et(st, et)
    for day in days:
        roledatas = getRoleFromHiveFile(day=day, groups=groups,timeRange = timeRange,roles=roles,EMPLOYEES=EMPLOYEES,minDur=minDur,maxDur=maxDur)
        logging.info('roledatas length %s'%len(roledatas))
        for shop_group , groupdatas in roledatas.items():
            shop_name = myRetailDatas.get_group_name(group=shop_group)
            fr = open('%s/macs.%s.%s.txt'%(exportPath,shop_name,day),'w')
            groupmacs = groupdatas.keys()
            fr.write('\n'.join(groupmacs))
            fr.close()

if __name__ == '__main__':
    import argparse,re,datetime,time
    parser = argparse.ArgumentParser(description='args')
    parser.add_argument('--st',metavar=u"starttime",default=None)
    parser.add_argument('--et',metavar="endTime",default=None)
    parser.add_argument('--groups',metavar="groups",default=None)
    parser.add_argument('--cross_groups',metavar="cross_groups",default=None)
    parser.add_argument('--comp_id',metavar="cmp_id",default=None)
    parser.add_argument('--roles',metavar="roles",default="1,-1,3,10")
    parser.add_argument('--min_dur',metavar="min dur ",default=0,type = int)
    parser.add_argument('--max_dur',metavar="max dur ",default=24*60*60,type = int)
    parser.add_argument('--employee',action='store_true',help='employee ')
    parser.add_argument('--export_path',metavar="",default=None)
    args = parser.parse_args()
    st = args.st
    et = args.et
    groups = args.groups.split(',')
    comp_id = args.comp_id
    roles = args.roles.split(',')
    cross_groups = args.cross_groups.split(',')
    minDur = args.min_dur
    maxDur = args.max_dur
    employee = args.employee
    exportPath = args.export_path
    #logging.basicConfig(level = logging.DEBUG)
    
    #
    #
    if employee and not exportPath:
        EMPLOYEES = getEmployee(st=st,et=et,comp_id=comp_id,roles=roles,timeRange=None)
        comp_group = myRetailDatas.get_comp_group(comp_id=comp_id)
        for mac in EMPLOYEES:
            print 'insert into mac_employee (dgroup,mac,type,last_time) VALUES("%s","%s",1,"%s")'%(comp_group,mac,datetime.date.today().strftime('%Y-%m-%d'))
    elif exportPath:
        EMPLOYEES = []
        export_mac(st=st,et=et,comp_id=comp_id,EMPLOYEES=EMPLOYEES,roles=roles,timeRange=None,minDur=minDur,maxDur=maxDur,exportPath = exportPath)
    else:
        
        EMPLOYEES = []
        stat_cross_shops_flow(st=st,et=et,first_groups=groups,cross_groups=cross_groups,comp_id=comp_id,EMPLOYEES=EMPLOYEES,roles=roles,timeRange=None,minDur=minDur,maxDur=maxDur)
    #

    